home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / gamma.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  168 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    gamma - 
  19.  *        Some support for gamma correction when reading and writing
  20.  *      color map entries.
  21.  *
  22.  *                Paul Haeberli - 1984
  23.  *
  24.  */
  25. #include "math.h"
  26. #include "port.h"
  27. #include "gl.h"
  28. #include "stdio.h"
  29.  
  30. static fixup();
  31.  
  32. FILE *configopen();
  33.  
  34. float gammacorrect();
  35. float ungammacorrect();
  36.  
  37. static unsigned char rgamtable[256];
  38. static unsigned char ggamtable[256];
  39. static unsigned char bgamtable[256];
  40. static unsigned char rungamtable[256];
  41. static unsigned char gungamtable[256];
  42. static unsigned char bungamtable[256];
  43. static short firsted;
  44.  
  45. #define MONITORGAMMA     2.8
  46.  
  47. gammapcolor(index,r,g,b)
  48. int index, r, g, b;
  49. {
  50.     mapcolor(index,r,g,b);
  51. }
  52.  
  53. static makegamtables()
  54. {
  55.     float gamval;
  56.     float val;
  57.     short i;
  58.     int rbal, gbal, bbal; 
  59.     float grbal, ggbal, gbbal; 
  60.  
  61.     gamval = getgamma();
  62.     getcolorbal(&rbal,&gbal,&bbal);
  63.     grbal = gammacorrect(rbal/255.0,MONITORGAMMA);
  64.     ggbal = gammacorrect(gbal/255.0,MONITORGAMMA);
  65.     gbbal = gammacorrect(bbal/255.0,MONITORGAMMA);
  66.     for (i=0; i<256; i++) {
  67.     rgamtable[i] = 255.0*gammacorrect((rbal*i)/(255.0*255.0),gamval)+0.49;
  68.     ggamtable[i] = 255.0*gammacorrect((gbal*i)/(255.0*255.0),gamval)+0.49;
  69.     bgamtable[i] = 255.0*gammacorrect((bbal*i)/(255.0*255.0),gamval)+0.49;
  70.     }
  71.     bzero(rungamtable,256);
  72.     bzero(gungamtable,256);
  73.     bzero(bungamtable,256);
  74.     for (i=0; i<256; i++) {
  75.     rungamtable[rgamtable[i]] = i;
  76.     gungamtable[ggamtable[i]] = i;
  77.     bungamtable[bgamtable[i]] = i;
  78.     }
  79.     fixup(rungamtable);
  80.     fixup(gungamtable);
  81.     fixup(bungamtable);
  82. }
  83.  
  84. static fixup(cptr)
  85. unsigned char *cptr;
  86. {
  87.     short i, lowval;
  88.  
  89.     lowval = 0;
  90.     for (i=256; i--; ) {
  91.     if (*cptr == 0) 
  92.         *cptr = lowval;
  93.     else
  94.         lowval = *cptr;
  95.     }
  96. }
  97.  
  98. gamgetmcolor(index,r,g,b)
  99. int index;
  100. short *r, *g, *b;
  101. {
  102.     getmcolor(index,r,g,b);
  103. }
  104.  
  105. float gammacorrect(i,gamma)
  106. float i, gamma;
  107. {
  108.     return pow(i,1.0/gamma);
  109. }
  110.  
  111. float ungammacorrect(i,gamma)
  112. float i, gamma;
  113. {
  114.     return pow(i,gamma);
  115. }
  116.  
  117. newgamma()
  118. {
  119.     firsted = firsted = 0;
  120. }
  121.  
  122. newgamtables()
  123. {
  124.     FILE *outf;
  125.     int i;
  126.     short rtab[256], gtab[256], btab[256];
  127.  
  128.     if ((outf = configopen(".gamtables","w")) == 0) {
  129.     fprintf(stderr,"couldn't open .gamtables\n");
  130.     return;
  131.     }
  132.     makegamtables();
  133.     fwrite(rgamtable,256,1,outf);
  134.     fwrite(ggamtable,256,1,outf);
  135.     fwrite(bgamtable,256,1,outf);
  136.     fwrite(rungamtable,256,1,outf);
  137.     fwrite(gungamtable,256,1,outf);
  138.     fwrite(bungamtable,256,1,outf);
  139.     fclose(outf);
  140.  
  141.     for(i=0; i<256; i++) {
  142.     rtab[i] = rgamtable[i];
  143.     gtab[i] = ggamtable[i];
  144.     btab[i] = bgamtable[i];
  145.     }
  146.     gammaramp(rtab,gtab,btab);
  147. }
  148.  
  149. readgamtables()
  150. {
  151.     FILE *inf;
  152.  
  153.     if ((inf = configopen(".gamtables","r")) == 0)  {
  154.     newgamtables();
  155.     if ((inf = configopen(".gamtables","r")) == 0)  {
  156.         fprintf(stderr,"couldn't open .gamtables\n");
  157.         return;
  158.     }
  159.     }
  160.     fread(rgamtable,256,1,inf);
  161.     fread(ggamtable,256,1,inf);
  162.     fread(bgamtable,256,1,inf);
  163.     fread(rungamtable,256,1,inf);
  164.     fread(gungamtable,256,1,inf);
  165.     fread(bungamtable,256,1,inf);
  166.     fclose(inf);
  167. }
  168.